/**
 * Created by Administrator on 2015-04-30.
 */
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

// 扩展 HttpServlet 类
public class SaveTestResult extends HttpServlet {
    ServletConfig config;
    public void init(ServletConfig config) throws ServletException
    {
        // 执行必需的初始化
        this.config = config;
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
//        response.setContentType("text/html");
        String jsonString = request.getParameter("jsonString"); //获取json字符串
        String ip = request.getRemoteAddr();
        Date date = new Date();
        String appPath = config.getServletContext().getRealPath("/");   //获取项目目录
        JSONObject object =  JSON.parseObject(jsonString);      //获取json对象
        JSONArray testPoints = object.getJSONArray("testPoints");
        object.put("ip", ip);

        object.remove("testPoints");
        object.put("caseFinallyResult", "pass");
        boolean t = false;  //是否将用例最终结果做失败替换
        for (int i = 0; i < testPoints.size(); i++){
            if (testPointsCompare(testPoints.getJSONObject(i)).equals("failed")){
                t = true;
            }
        }
        if (t){
            object.remove("caseFinallyResult");
            object.put("caseFinallyResult", "failed");
        }
        if (object.containsKey("result")){
            if (object.getString("result").equals("-1")){
                object.remove("caseFinallyResult");
                object.put("caseFinallyResult", "failed");
            }
        }
        object.put("testPoints", testPoints);


//      组装测试数据
        String dataString = "[" + date + "] ";
        String jsonStr = "json:" + object.toString();
        String resultString = dataString + jsonStr + "\n";

//      创建测试结果文件,并写入
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = simpleDateFormat.format(new Date());
        String fullTestLogPath = appPath + "//testResult//";
        File file = new File(fullTestLogPath + formatDate);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file, true);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
            outputStreamWriter.write(resultString);
            outputStreamWriter.flush();
            outputStreamWriter.close();

//            FileWriter fileWriter = new FileWriter(file.getAbsolutePath(), true);
//            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//            bufferedWriter.write(resultString);
//            bufferedWriter.close();
        }catch (IOException e){
            e.printStackTrace();
        }


        PrintWriter out = response.getWriter();
        out.println("1");
        System.out.println("save test result. " + object.toString());
    }

    public void destroy()
    {
        // 什么也不做
    }

    private String testPointsCompare(JSONObject jsonObject){
//        开始比较测试对象
//        compareMark  ==eq  !=ne, <lt,   >gt,   <=le,   >=ge
        String compareMark = jsonObject.getString("compareMark");
        String testResult = jsonObject.getString("testResult");
        String expectResult = jsonObject.getString("expectResult");
        String returnStr = "pass";

        if ((compareMark != null) && compareMark.equals("eq")){   //要么相等
            if(testResult.equals(expectResult)) {
                jsonObject.put("testPointResult", "pass");
            }else {
                jsonObject.put("testPointResult", "failed");
                returnStr = "failed";
            }
        }
        if ((compareMark != null) && compareMark.equals("ne")){
            if(testResult.equals(expectResult)) {
                jsonObject.put("testPointResult", "failed");
                returnStr = "failed";
            }else {
                jsonObject.put("testPointResult", "pass");
            }
        }
        //要么不想等
        try {
            int testResultInteger, expectResultInteger;
            String[] compareMarks = {"lt", "gt", "le", "ge"};
            for (String aCompareMark: compareMarks){
                if (aCompareMark.equals(compareMark)){
                    testResultInteger = Integer.valueOf(testResult);
                    expectResultInteger = Integer.valueOf(expectResult);

                    if (compareMark.equals("lt")){
                        if (expectResultInteger < testResultInteger){
                            jsonObject.put("testPointResult", "pass");
                        }else {
                            jsonObject.put("testPointResult", "failed");
                            returnStr = "failed";
                        }
                    }
                    if (compareMark.equals("gt")){
                        if (expectResultInteger > testResultInteger){
                            jsonObject.put("testPointResult", "pass");
                        }else {
                            jsonObject.put("testPointResult", "failed");
                            returnStr = "failed";
                        }
                    }
                    if (compareMark.equals("le")){
                        if (expectResultInteger <= testResultInteger){
                            jsonObject.put("testPointResult", "pass");
                        }else {
                            jsonObject.put("testPointResult", "failed");
                            returnStr = "failed";
                        }
                    }
                    if (compareMark.equals("ge")){
                        if (expectResultInteger >= testResultInteger){
                            jsonObject.put("testPointResult", "pass");
                        }else {
                            jsonObject.put("testPointResult", "failed");
                            returnStr = "failed";
                        }
                    }
                }
            }
        }catch (Exception e){
            jsonObject.put("testPointResult", "failed");
            returnStr = "failed";
        }
        return returnStr;
    }
}